/**
 *****************************************************************************
 * DESC ：. 
 * Author : Junwei Zhang 
 * CREATEDATE ：2012-8-19 
 * MODIFYLIST ：       	Name 			 Date 			Reason/Contents
 * 				------------------------------------------------------ 
 * 				 	Junwei Zhang     2012-8-19		Created.
 ****************************************************************************
 **/
package com.me.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

/**
 * 消息提示框. <br/>
 * 上午10:25:28 2012-8-19
 * 
 * @Author <a href="mailto:mestudying@sina.com">Junwei Zhang</a><br/>
 **/
public class NotificationWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	public static final int TOP_LEFT = 0;
	public static final int TOP_RIGHT = 1;
	public static final int BOTTOM_LEFT = 2;
	public static final int BOTTOM_RIGHT = 3;
	
	public static final int WIDTH = 100;
	public static final int HEIGHT = 40;

	private JTalkClientFront parent = null;
	private JLabel jLabelText;
	private JButton jButtonOK;
	private String memberName = null;
	private int myPosition = BOTTOM_RIGHT;
	private AnimationThread animationThread = null;

	public NotificationWindow(JTalkClientFront parent) {
		this.parent = parent;
		this.initComponents();
	}

	/**
	 * 初始化组件.
	 */
	private void initComponents() {
		this.setTitle("New Message");
		this.setFocusable(false);
		this.setFocusableWindowState(false);
		this.setAlwaysOnTop(true);
		this.setUndecorated(true);

		jLabelText = new JLabel();
		jLabelText.setBackground(new Color(255, 255, 51));
		jLabelText.setFont(new Font("Dialog", 0, 10));
		jLabelText.setForeground(new Color(0, 0, 0));
		jLabelText.setHorizontalAlignment(SwingConstants.CENTER);
		jLabelText.setText("You have a new Message!");
		jLabelText.setHorizontalTextPosition(SwingConstants.CENTER);
		jLabelText.setOpaque(true);
		this.getContentPane().add(jLabelText, BorderLayout.NORTH);

		jButtonOK = new JButton();
		jButtonOK.setText("Show");
		jButtonOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clickOK();
			}
		});

		this.getContentPane().add(jButtonOK, BorderLayout.CENTER);
		this.pack();
	}

	/**
	 * 设置提示框在桌面显示位置.
	 * @param position
	 */
	public void setPosition(int position) {
		this.myPosition = position;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		switch (position) {
		case NotificationWindow.TOP_LEFT:
			this.setBounds(0, 0, WIDTH, HEIGHT);
			break;
		case NotificationWindow.TOP_RIGHT:
			this.setBounds(screenSize.width - WIDTH, 0, WIDTH, HEIGHT);
			break;
		case NotificationWindow.BOTTOM_LEFT:
			this.setBounds(0, screenSize.height - HEIGHT, WIDTH, HEIGHT);
			break;
		case NotificationWindow.BOTTOM_RIGHT:
			this.setBounds(screenSize.width - WIDTH, screenSize.height - HEIGHT, WIDTH, HEIGHT);
			break;
		}
	}

	public void setMemberName(String name) {
		if(name == null){
			this.memberName = "";
			return;
		}
		this.memberName = name;
		this.jLabelText.setText(this.memberName);
	}

	private void clickOK() {
		this.parent.toFront();
		if (this.memberName != null && !"".equals(this.memberName)) {
			this.parent.theClient.getMemberByName(this.memberName).showChatWindow(this.parent);
		}
		this.setPosition(this.myPosition);
		this.dispose();
	}
	
	@Override
	public void setVisible(boolean yes) {
		if(this.isVisible() == yes){
			return;
		}
		if(this.animationThread != null && this.animationThread.isRunning()){
			return;
		}
		if(this.animationThread != null){
			animationThread.stopAnimation();
		}
		this.animationThread  = new AnimationThread(this);
		this.animationThread.start();
		super.setVisible(yes);
		this.setPosition(this.myPosition);
	}
	
	@Override
	public void dispose() {
		if(this.animationThread != null){
			this.animationThread.stopAnimation();
		}
		super.dispose();
	}
	
	public class AnimationThread extends Thread{
		private static final int STEP_SIZE = 1;
		private static final int MOVE_AREA = 50;
		private NotificationWindow parent = null;
		private int stepY = STEP_SIZE;
		private boolean isRunning = true;

		public AnimationThread(NotificationWindow parent) {
			this.parent = parent;
			if(parent.myPosition == NotificationWindow.TOP_LEFT){
				this.stepY  *= -1;
			}
		}

		public boolean isRunning() {
			return this.isRunning;
		}

		public void stopAnimation() {
			this.isRunning  = false;
		}

		@Override
		public void run() {
			int runs = MOVE_AREA;
			while (runs > 0 && this.isRunning) {
				this.parent.setLocation(this.parent.getLocation().x, this.parent.getLocation().y - stepY);
				try {
					Thread.sleep(100);
				} catch (Exception e) {
				}

				runs--;

				if (runs == (int) (MOVE_AREA / 2)) {
					this.isRunning = false;
					this.parent.setPosition(this.parent.myPosition);
				}
			}
			this.isRunning = false;
			this.parent.dispose();
		}
		
	}

}
